#include <stdio.h>
#define Size 15
#define TRUE 1
#define FALSE 0
void InitBoard(char (*Board)[Size])
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            Board[i][j] = '-';
        }
    }
}

void PrintBoard(char (*Board)[Size])
{
    printf("     ");
    for (int i = 0; i < Size; i++)
    {
        printf("%2d  ", i + 1);
    }
    printf("\n");
    for (int i = 0; i < Size; i++)
    {
        printf("%2d ", i + 1);
        for (int j = 0; j < Size; j++)
        {
            printf("%4c", Board[i][j]);
        }
        printf("\n");
    }
}

int MakeMove(char (*Board)[Size], int row, int column, char player)
{
    if (row < 0 || row >= Size || column < 0 || column >= Size)
    {
        printf("invalid place!please try again!");
        return FALSE;
    }
    if (Board[row][column] != '-')
    {
        printf("this place have no place!try again!\n");
        return FALSE;
    }
    Board[row][column] = player;
    return TRUE;
}

int CheckIsWin(char (*Board)[Size], char player,int row,int column)
{
    // Judge the chess around
    int direction[4][2] = {{1, 0},
                           {0, 1},
                           {1, 1},
                           {-1, 1}};
    for (int i = 0; i < 4; i++)
    {
        int count = 1;
        int dx = row + direction[i][0];
        int dy = column + direction[i][1];
        while (dx >= 0 && dx < Size && dy >= 0 && dy < Size && Board[dx][dy] == player)
        {
            count++;
            dx = dx + direction[i][0];
            dy = dy + direction[i][1];

            if(count>=5)
            return TRUE;
        }
        dx = row - direction[i][0];
        dy = row - direction[i][1];
        while (dx >= 0 && dx < Size && dy >= 0 && dy < Size && Board[dx][dy] == player)
        {
            count++;
            dx = dx - direction[i][0];
            dy = dy - direction[i][1];
        }
        if(count>=5)
        return TRUE;
    }
    return FALSE;
}
int CheckIsDraw(char (*Board)[COLUMN])
{
    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COLUMN; j++)
        {
            if (Board[i][j] == ' ')
                return FALSE;
        }
    }
    return TRUE;
}
int main()
{
    char Board[Size][Size] = {0};
    InitBoard(Board);
    // PrintBoard(Board);

    char player = 'X';
    while (1)
    {
        PrintBoard(Board);
        int row = 0, column = 0;
        printf("please input a place to make move:");
        scanf("%d %d", &row, &column);
        if (MakeMove(Board, row - 1, column - 1, player) == FALSE)
        {
            continue;
        }
        if (CheckIsWin(Board,player,row-1,column-1) == TRUE)
        {
            PrintBoard(Board);
            printf("player %c wins!Congratulations!\n", player);
            break;
        }
        if(CheckIsDraw(Board)==TRUE)
        {
            PrintBoard(Board);
            printf("Draw!Game Over!\n");
            break;
        }
        player = (player == 'X') ? ('O') : ('X');
    }
    return 0;
}